package com.qyzmt.service.impl;

import com.qyzmt.pojo.*;
import com.qyzmt.service.CalculatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CalculatorServiceImpl implements CalculatorService {

    @Override
    public ShoppingResult shopping(Shopping shopping) {
        double shouldReturnCoin = 0;  // 应退金额
        String basisLaws = "暂无依据";

        double originalPrice = shopping.getOriginalPrice(); // 商品原价
        double actualPayment = shopping.getActualPayment(); // 实际付款金额
        String rightsType = shopping.getRightsType();  // 维权类型
        int usedDays = shopping.getUsedDays(); // 使用天数 已使用时常
        int expectedDays = shopping.getExpectedDays(); // 预计使用天数
        String damageLevel = shopping.getDamageLevel();  // 商品损坏程度

        // 数据校验
        if(originalPrice < 0 || actualPayment < 0 || usedDays < 0 || expectedDays < 0){
            shouldReturnCoin = -1;
            basisLaws = "数据不符合现实！请检查您输入的数据！";
            ShoppingResult shoppingResult = new ShoppingResult(shouldReturnCoin,basisLaws);
            return shoppingResult;
        }

        if(rightsType.equals("退款退货")){
            shouldReturnCoin = actualPayment - (originalPrice / expectedDays * usedDays);
            basisLaws = "【计算结果以无损坏为准】质量问题退款:依据《中华人民共和国消费者权益保护法》第二十四条";
            log.info("智能计算-消费购物维权计算器,用户选择类型:{},计算值:{}","退款退货",shouldReturnCoin);
        }

        if(rightsType.equals("假一赔三")){
            shouldReturnCoin = originalPrice * 3 + actualPayment;
            basisLaws = "【计算结果以无损坏为准】假一赔三场景:依据《中华人民共和国消费者权益保护法》第五十五条";
            log.info("智能计算-消费购物维权计算器,用户选择类型:{},计算值:{}","假一赔三",shouldReturnCoin);
        }

        if(rightsType.equals("瑕疵赔偿")){
            double damagelevel = 0;
            if(damageLevel.equals("0%")){
                damagelevel = 0;
            }
            else if(damageLevel.equals("10%")){
                damagelevel = 0.1;
            }
            else if(damageLevel.equals("20%")){
                damagelevel = 0.2;
            }
            else if(damageLevel.equals("30%")){
                damagelevel = 0.3;
            }
            else if(damageLevel.equals("40%")){
                damagelevel = 0.4;
            }
            else if(damageLevel.equals("50%")){
                damagelevel = 0.5;
            }

            shouldReturnCoin = originalPrice * damagelevel;
            basisLaws  = "瑕疵赔偿场景:依据《中华人民共和国消费者权益保护法》第二十四条及行业惯例";
            log.info("智能计算-消费购物维权计算器,用户选择类型:{},计算值:{}","瑕疵赔偿",shouldReturnCoin);
        }

        if(shouldReturnCoin < 0){
            shouldReturnCoin = -1;
            basisLaws = "数据不符合现实！请检查您输入的数据！";
        }

        ShoppingResult shoppingResult = new ShoppingResult(shouldReturnCoin,basisLaws);
        return shoppingResult;

    }

    @Override
    public LaborResult labor(Labor labor) {
        double shouldPay = 0;
        double compensation = 0;
        double total = 0;
        String exp = "未签协议不影响薪资追索，可主张全额支付"; // 详细说明

        String salaryType = labor.getSalaryType(); // 时薪 日薪 月薪
        double salaryStandard = labor.getSalaryStandard();  // 约定薪资标准
        int delayDays = labor.getDelayDays(); // 拖欠时长
        int workDays = labor.getWorkDays(); // 当月工作日
        int actualWorkTime = labor.getActualWorkTime(); // 实际工作时长

        //数据校验
        if(salaryStandard < 0 || delayDays < 0 || workDays < 0 || actualWorkTime < 0){
            shouldPay = -1;
            compensation = -1;
            total = -1;
            exp = "数据不符合现实！请检查您输入的数据！";
            LaborResult laborResult = new LaborResult(shouldPay,compensation,total,exp);
            return laborResult;
        }


        if(salaryType.equals("时薪")){
            shouldPay = salaryStandard * actualWorkTime;
        }
        else if(salaryType.equals("日薪")){
            shouldPay = salaryStandard * actualWorkTime;
        }
        else if(salaryType.equals("月薪")){
            shouldPay = salaryStandard / workDays * actualWorkTime;
        }
        if(delayDays > 3){
            compensation = shouldPay * 0.5;
        }
        else{
            compensation = 0;
        }

        total = shouldPay + compensation;
        if(shouldPay < 0 || compensation < 0 || total < 0){
            shouldPay = -1;
            compensation = -1;
            total = -1;
            exp = "数据不符合现实！请检查您输入的数据！";
        }

        LaborResult laborResult = new LaborResult(shouldPay,compensation,total,exp);
        return laborResult;
    }

    @Override
    public RentalResult rental(Rental rental) {
        double refund = 0; // 应退押金
        double settlement = 0; // 费用结算金额
        double total = 0;  // 合计应得金额
        String explanation = "自然损耗无需扣押金，人为损坏需按实际维修价扣减。";  // 明细说明

        double monthlyRent = rental.getMonthlyRent(); // 月租金
        double deposit = rental.getDeposit();  // 支付押金
        double rentalMonths = rental.getRentalMonths(); // 租房时长 月
        double penalty = rental.getPenalty(); // 合同违约金
        String  checkoutType = rental.getCheckoutType();  // 退房类型 正常退房 提前退租 房东违约
        String damageCondition = rental.getDamageCondition();  // 房屋损坏情况 无损坏 自然损耗 人为损坏
        double paidFees = rental.getPaidFees();    // 已缴物业水电费
        double actualFees = rental.getActualFees(); // 实际产生物业水电费

        //数据校验
        if(monthlyRent < 0 || deposit < 0 || rentalMonths < 0 || penalty < 0 || paidFees < 0 || actualFees < 0){
            refund = -1;
            settlement = -1;
            total = -1;
            explanation = "数据不符合现实！请检查您输入的数据！";
            RentalResult rentalResult = new RentalResult(refund,settlement,total,explanation);
            return rentalResult;
        }

        if(checkoutType.equals("正常退房")){
            refund = deposit;
            log.info("用户选择正常退房.");
        }
        else if(checkoutType.equals("提前退租")){
            refund = deposit - (penalty * monthlyRent);
            log.info("用户选择提前退租,应退押金:{}",refund);
        }
        else if(checkoutType.equals("房东违约")){
            // 赔偿金额=月租金×2 + 全额押金（按“房东提前收房”常见赔偿标准）
            refund = monthlyRent * 2 + deposit;
            log.info("用户选择房东违约,应退押金:{}",refund);
        }

        if(damageCondition.equals("无损坏") || damageCondition.equals("自然损耗")){
            explanation = "无损坏或自然损耗无需扣押金。";
        }
        else if(damageCondition.equals("人为损坏")){
            explanation = "人为损坏需按实际维修价扣减押金，请手动计算。";
        }

        // 费用结算：应退/应补费用=已缴物业费/水电费 - 实际产生费用
        settlement = paidFees - actualFees;
        total = refund + settlement;
        if(refund < 0 || settlement < 0 || total < 0){
            refund = -1;
            settlement = -1;
            total = -1;
            explanation = "数据不符合现实！请检查您输入的数据！";
        }
        RentalResult rentalResult = new RentalResult(refund,settlement,total,explanation);
        return rentalResult;
    }

    @Override
    public StudyAbroadResult studyAbroad(StudyAbroad studyAbroad) {
        double refund = 0; // 应退费用金额（元）
        double serviceFee = 0; // 手续费
        double actualAmount = 0; // 实际到账金额（元）
        String explanation = ""; // 明细说明

        String feeType = studyAbroad.getFeeType(); // 已缴费用类型（下拉选择：学费/中介费/语言培训费）
        double totalFee = studyAbroad.getTotalFee(); // 已缴总金额（元）
        String refundReason = studyAbroad.getRefundReason(); // 退费触发原因（下拉选择：未入学/入学后退学/机构违约）
        int serviceProgress = studyAbroad.getServiceProgress(); // 已服务进度（输入框后面标单位：%）

        if(totalFee < 0 || serviceProgress < 0){
            refund = -1;
            serviceFee = -1;
            actualAmount = -1;
            explanation = "数据不符合现实！请检查您输入的数据！";
        }

        if(refundReason.equals("未入学")){
            refund = totalFee;
            serviceFee = refund * 0.05;
            actualAmount = totalFee - serviceFee;
            explanation = "手续费按照最高5%计算，实际手续费可能更低；需依据合同约定比例，无合同按‘已服务比例反向折算’";
        }
        else if(refundReason.equals("入学后退学")){
            double jindu = serviceProgress * 0.01;
            refund = totalFee * (1 - jindu);
            serviceFee = 0;
            actualAmount = refund;
            explanation = "无手续费。需依据合同约定比例，无合同按‘已服务比例反向折算’";
        }
        else if(refundReason.equals("机构违约")){
            refund = totalFee * 1.2;
            serviceFee = 0;
            actualAmount = refund;
            explanation = "无手续费。需依据合同约定比例，无合同按‘已服务比例反向折算’";
        }

        //结果校验
        if(refund < 0 || serviceFee < 0 || actualAmount < 0){
            refund = -1;
            serviceFee = -1;
            actualAmount = -1;
            explanation = "数据不符合现实！请检查您输入的数据！";
        }

        StudyAbroadResult studyAbroadResult = new StudyAbroadResult(refund, serviceFee, actualAmount, explanation);
        return studyAbroadResult;
    }
}
